package com.clothing.dao.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.clothing.dao.FriendDao;
import com.clothing.entity.Friend;
import com.clothing.entity.Friend.FriendStatus;
import com.clothing.entity.Friend.Status;
import com.clothing.utils.Utils;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

/**
 * @ClassName: FriendDaoImpl
 * @Description: TODO(Dao)
 * @author Charles.Tao
 * @date 2017年9月6日 下午3:40:20
 * @version 1.0
 */
@Repository
public class FriendDaoImpl extends BaseDaoImpl<Friend, Long>
        implements FriendDao
{
    @Override
    public Friend findFriend(Long userOne, Long userTwo)
    {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Friend> criteriaQuery = criteriaBuilder
            .createQuery(Friend.class);
        Root<Friend> root = criteriaQuery.from(Friend.class);
        criteriaQuery.select(root);
        Long smallUserId = 0L;
        Long bigUserId = 0L;

        if (userOne < userTwo)
        {
            smallUserId = userOne;
            bigUserId = userTwo;
        }
        else
        {
            smallUserId = userTwo;
            bigUserId = userOne;
        }
        
        Predicate predicate = criteriaBuilder.and(criteriaBuilder
            .equal(root.get("bigUser").get("id"), bigUserId), criteriaBuilder
            .equal(root.get("smallUser").get("id"), smallUserId));
        criteriaQuery.where(predicate);
        List<Friend> friends = findList(criteriaQuery, null, 1, null, null);

        if (Utils.isEmpty(friends))
        {
            return null;
        }

        return friends.get(0);
    }

    @Override
    public Page<Friend> findFriends(Long userId, FriendStatus status, Pageable pageable)
    {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Friend> criteriaQuery = criteriaBuilder
            .createQuery(Friend.class);
        Root<Friend> root = criteriaQuery.from(Friend.class);
        criteriaQuery.select(root);
        Path<Status> statusPath = root.get("status");
        Path<Long> smallPath= root.get("smallUser").get("id");
        Path<Long> bigPath= root.get("bigUser").get("id");
        Predicate predicate = null;
        
        if (FriendStatus.attention == status)
        {
            predicate = criteriaBuilder.or(criteriaBuilder
                .and(criteriaBuilder
                    .equal(smallPath, userId), criteriaBuilder
                    .equal(statusPath, Status.small_to_big)), criteriaBuilder
                .and(criteriaBuilder
                    .equal(bigPath, userId), criteriaBuilder
                    .equal(statusPath, Status.big_to_small)));
        }
        else if (FriendStatus.fans == status)
        {
            predicate = criteriaBuilder.or(criteriaBuilder
                .and(criteriaBuilder
                    .equal(bigPath, userId), criteriaBuilder
                    .equal(statusPath, Status.small_to_big)), criteriaBuilder
                .and(criteriaBuilder
                    .equal(smallPath, userId), criteriaBuilder
                    .equal(statusPath, Status.big_to_small)));
        }
        else
        {
            predicate = criteriaBuilder.and(criteriaBuilder
                .equal(statusPath, Status.mutual),criteriaBuilder
                .or(criteriaBuilder
                    .equal(bigPath, userId), criteriaBuilder
                    .equal(smallPath, userId)));
        }
        
        criteriaQuery.where(predicate);
        
        return findPage(criteriaQuery, pageable);
    }
}
